<!DOCTYPE html><html lang="zh-CN" data-theme="dark"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>手写一个简版的promise | 北城韩雨</title><meta name="keywords" content="JavaScript"><meta name="author" content="北城韩雨"><meta name="copyright" content="北城韩雨"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#0d0d0d"><meta name="description" content="JavaScript 源码">
<meta property="og:type" content="article">
<meta property="og:title" content="手写一个简版的promise">
<meta property="og:url" content="http://example.com/2020/04/27/%E5%86%99%E4%B8%80%E4%B8%AA%E7%AE%80%E7%89%88%E7%9A%84Promise/index.html">
<meta property="og:site_name" content="北城韩雨">
<meta property="og:description" content="JavaScript 源码">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2017-07-20%2F4f06f9aa2ebcea6691a78537052e8e9f%2F00001.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530599&t=32fb97bad7007bdc6c712ccadacd5721">
<meta property="article:published_time" content="2020-04-26T16:00:00.000Z">
<meta property="article:modified_time" content="2019-06-02T16:00:00.000Z">
<meta property="article:author" content="北城韩雨">
<meta property="article:tag" content="JavaScript">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2017-07-20%2F4f06f9aa2ebcea6691a78537052e8e9f%2F00001.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530599&t=32fb97bad7007bdc6c712ccadacd5721"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="http://example.com/2020/04/27/%E5%86%99%E4%B8%80%E4%B8%AA%E7%AE%80%E7%89%88%E7%9A%84Promise/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'mediumZoom',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '手写一个简版的promise',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2019-06-03 00:00:00'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
    const detectApple = () => {
      if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/custom.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="/css/coin.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="/css/liuxing.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/l-lin/font-awesome-animation/dist/font-awesome-animation.min.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swiper/swiper-bundle.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/zhheo/JS-Heo@main/bb/showbb_in_index.css"><!-- hexo injector head_end start --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" media="print" onload="this.media='screen'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/l-lin/font-awesome-animation/dist/font-awesome-animation.min.css" media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hexo-butterfly-tag-plugins-plus/lib/tag_plugins.min.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --><meta name="generator" content="Hexo 5.3.0"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/my.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">65</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">23</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">21</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/"><i class="fa-fw fas fa-home faa-tada"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/shuoshuo/"><i class="fa-fw fas fa-archive faa-tada"></i><span> 叨逼叨</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/tags/"><i class="fa-fw fas fa-tags faa-tada"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/categories/"><i class="fa-fw fas fa-folder-open faa-tada"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/link/"><i class="fa-fw fas fa-link faa-tada"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/comments/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-message"></use></svg><span> 留言板</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/about/"><i class="fa-fw fas fa-heart faa-tada"></i><span> 个人简介</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2017-07-20%2F4f06f9aa2ebcea6691a78537052e8e9f%2F00001.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530599&amp;t=32fb97bad7007bdc6c712ccadacd5721')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">北城韩雨</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/"><i class="fa-fw fas fa-home faa-tada"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/shuoshuo/"><i class="fa-fw fas fa-archive faa-tada"></i><span> 叨逼叨</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/tags/"><i class="fa-fw fas fa-tags faa-tada"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/categories/"><i class="fa-fw fas fa-folder-open faa-tada"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/link/"><i class="fa-fw fas fa-link faa-tada"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/comments/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-message"></use></svg><span> 留言板</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/about/"><i class="fa-fw fas fa-heart faa-tada"></i><span> 个人简介</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">手写一个简版的promise</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2020-04-26T16:00:00.000Z" title="发表于 2020-04-27 00:00:00">2020-04-27</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2019-06-02T16:00:00.000Z" title="更新于 2019-06-03 00:00:00">2019-06-03</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%BA%90%E7%A0%81/">源码</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">3.2k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>12分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="手写一个简版的promise"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="手写一个简单版的Promise"><a href="#手写一个简单版的Promise" class="headerlink" title="手写一个简单版的Promise"></a>手写一个简单版的Promise</h1><h2 id="Promise都做了哪些事情"><a href="#Promise都做了哪些事情" class="headerlink" title="Promise都做了哪些事情"></a>Promise都做了哪些事情</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 首先我们 <span class="keyword">new</span> 的时候 会穿进去一个立即执行的函数 如果这个函数不传 就会报错</span><br><span class="line"><span class="number">2.</span> 在类的实例上 会有两个值 一个是状态 初始值是 pending 一个是值 初始值 为 <span class="literal">undefined</span></span><br><span class="line"><span class="number">3.</span> 这个立即执行函数 会接受两个参数 一个参数是 resolve 函数 会把我们类里面的状态变为成功 fulfilled 并且把我的值改变为你穿进来的值 一个是 reject 函数 会把我们类里面的状态变成为失败 rejected 值也会跟着改变</span><br><span class="line"><span class="number">4.</span> 做为对象的时候 调用<span class="built_in">Promise</span>.resovle方法会返回一个状态为成功的promise对象 调用<span class="built_in">Promise</span>.reject 方法会返回一个状态为失败的promise对象</span><br><span class="line"><span class="number">5.</span> 而不论成功或者失败 当你用resolve 或者 reject 的时候 都会通知then方法的里面的成功 或者失败的函数 异步的去获得结果</span><br></pre></td></tr></table></figure>
<h2 id="作为构造函数"><a href="#作为构造函数" class="headerlink" title="作为构造函数"></a>作为构造函数</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 首先 在类上 会有 状态 初始是pending 而 默认值 是<span class="literal">undefined</span></span><br><span class="line"><span class="number">2.</span> 然后穿进来的立即执行函数 如果不传 就会报错 这个函数会被调用 然后传出去两个实参 一个是 成功的resolve函数 一个是 失败的reject函数</span><br><span class="line"><span class="number">3.</span> 我们还需要写一个change 函数 用来改变你的状态 和 你的 默认值 调用resolve 函数的时候因为是成功 我们就在change里传入成功的状态 fulfilled 而失败 正好相反</span><br><span class="line"><span class="number">4.</span> 一旦我们的状态改变了 就不能在改变了</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">MyPromise</span>(<span class="params">executor</span>) </span>&#123;</span><br><span class="line">    <span class="comment">//1.这个立即执行函数我们必须要传入 如果不穿就报错</span></span><br><span class="line">    <span class="keyword">if</span>(<span class="keyword">typeof</span> executor !== <span class="string">&#x27;function&#x27;</span>) <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">TypeError</span>(<span class="string">&#x27;MyPromise resolver undefined is not a function&#x27;</span>)</span><br><span class="line">    <span class="comment">//2.定义我们的初始状态 和我们的默认值</span></span><br><span class="line">    <span class="built_in">this</span>.MyPromiseState = <span class="string">&#x27;pending&#x27;</span></span><br><span class="line">    <span class="built_in">this</span>.MyPromiseValue = <span class="literal">undefined</span></span><br><span class="line">    <span class="comment">//3. 然后这个立即执行函数会执行 参数为两个函数 一个是成功的函数 resolve 一个是失败的函数 reject</span></span><br><span class="line">    <span class="keyword">var</span> resolve = <span class="function"><span class="keyword">function</span> <span class="title">resolve</span> (<span class="params">result</span>)</span>&#123;</span><br><span class="line">    <span class="comment">//4. 当我们 new Promise((resolve,reject) =&gt; &#123; resolve(ok)&#125;) 成功的时候会执行这个函数 改变状态为成功并且赋值</span></span><br><span class="line">        change(<span class="string">&#x27;fulfilled&#x27;</span>,result)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">var</span> reject = <span class="function"><span class="keyword">function</span> <span class="title">reject</span> (<span class="params">reason</span>)</span>&#123;</span><br><span class="line">        change(<span class="string">&#x27;rejected&#x27;</span>,reason)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//5.这里当我们调用resolve的时候 由于前面没有任何东西 我们要保证我们change函数里面的this指向正确 所以要缓存一下</span></span><br><span class="line">    <span class="keyword">var</span> _this = <span class="built_in">this</span></span><br><span class="line">    <span class="keyword">var</span> change = <span class="function"><span class="keyword">function</span> <span class="title">change</span>(<span class="params">state,value</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(_this.MyPromiseState !== <span class="string">&#x27;pending&#x27;</span>) <span class="keyword">return</span></span><br><span class="line">      _this.MyPromiseState = state</span><br><span class="line">      _this.MyPromiseValue = value</span><br><span class="line">    &#125;</span><br><span class="line">    executor(resolve,reject)</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> p1 = <span class="keyword">new</span> MyPromise(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">  resovle(<span class="string">&#x27;ok&#x27;</span>)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h2 id="作为普通对象"><a href="#作为普通对象" class="headerlink" title="作为普通对象"></a>作为普通对象</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 会有<span class="number">4</span>个方法 分别是resolve reject all(传递一个 promise实例组成的数组 当数组中所有的实例都返回成功的时候才会成功) race 这里我们现在暂时实现 <span class="built_in">Promise</span>.resolve() <span class="built_in">Promise</span>.reject()</span><br><span class="line"><span class="number">2.</span> resolve 会返回一个成功的 <span class="built_in">Promise</span>实例 </span><br><span class="line"><span class="number">3.</span> reject  会返回一个失败的 <span class="built_in">Promise</span>实例 </span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">MyPromise.resolve = <span class="function"><span class="keyword">function</span> <span class="title">resolve</span> (<span class="params">result</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> MyPromise(<span class="function"><span class="keyword">function</span> (<span class="params">resolve</span>)</span>&#123;</span><br><span class="line">        resolve(result)</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br><span class="line">MyPromise.reject = <span class="function"><span class="keyword">function</span> <span class="title">reject</span> (<span class="params">reason</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> MyPromise(<span class="function"><span class="keyword">function</span>(<span class="params">_,reject</span>)</span>&#123;</span><br><span class="line">        reject(reason)</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="then的时候执行"><a href="#then的时候执行" class="headerlink" title="then的时候执行"></a>then的时候执行</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 我们上面的类 写完之后都是立即执行的 也就是 当你调用resolve函数的以后 就已经把状态改变</span><br><span class="line"><span class="number">2.</span> 但是我们要通过then 拿到结果 这个里面是异步的微任务 这里我们用定时器模拟</span><br><span class="line"><span class="number">3.</span> then 是公共方法 所以我们要写在原型</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//如果 我们then 里什么也没有传 我们就需要默认给补充一个</span></span><br><span class="line"><span class="keyword">var</span> p1 = <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="keyword">function</span>(<span class="params">resolve,reject</span>)</span>&#123;</span><br><span class="line">  resolve(res)</span><br><span class="line">&#125;)</span><br><span class="line">p1.then(<span class="literal">null</span>,<span class="literal">null</span>) 就相当于下面的情况 所以我们要在then执行的时候判断一个 如果没传就补充上</span><br><span class="line">p1.then(<span class="function"><span class="keyword">function</span>(<span class="params">res</span>)</span>&#123;</span><br><span class="line">  <span class="built_in">Promise</span>.resolve(res)</span><br><span class="line">&#125;,<span class="function"><span class="keyword">function</span>(<span class="params">reason</span>)</span>&#123;</span><br><span class="line">   <span class="built_in">Promise</span>.reject(reason)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">MyPromise</span>(<span class="params">executor</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(<span class="keyword">typeof</span> executor !== <span class="string">&#x27;function&#x27;</span>) <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">TypeError</span>(<span class="string">&#x27;MyPromise resolver undefined is not a function&#x27;</span>)</span><br><span class="line">    <span class="built_in">this</span>.MyPromiseState = <span class="string">&#x27;pending&#x27;</span></span><br><span class="line">    <span class="built_in">this</span>.MyPromiseValue = <span class="literal">undefined</span></span><br><span class="line">    <span class="comment">//1.为了then能拿到我们的状态所以在上面定义了这两个方法</span></span><br><span class="line">    <span class="built_in">this</span>.resolveFunction = <span class="function"><span class="keyword">function</span> <span class="title">rejectFunction</span> (<span class="params"></span>)  </span>&#123;&#125;</span><br><span class="line">    <span class="built_in">this</span>.rejectFunction = <span class="function"><span class="keyword">function</span> <span class="title">rejectFunction</span> (<span class="params"></span>) </span>&#123;&#125;</span><br><span class="line">    <span class="keyword">var</span> resolve = <span class="function"><span class="keyword">function</span> <span class="title">resolve</span> (<span class="params">result</span>)</span>&#123;</span><br><span class="line">        change(<span class="string">&#x27;fulfilled&#x27;</span>,result)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">var</span> reject = <span class="function"><span class="keyword">function</span> <span class="title">reject</span> (<span class="params">reason</span>)</span>&#123;</span><br><span class="line">        change(<span class="string">&#x27;rejected&#x27;</span>,reason)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">var</span> _this = <span class="built_in">this</span></span><br><span class="line">    <span class="keyword">var</span> change = <span class="function"><span class="keyword">function</span> <span class="title">change</span>(<span class="params">state,value</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(_this.MyPromiseState !== <span class="string">&#x27;pending&#x27;</span>) <span class="keyword">return</span></span><br><span class="line">    _this.MyPromiseState = state</span><br><span class="line">    _this.MyPromiseValue = value</span><br><span class="line">    <span class="comment">//2. 由于then的时候 是异步的微任务 这里我们就用宏任务模拟一下</span></span><br><span class="line">    <span class="built_in">setTimeout</span>(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="comment">//3.这里 就是当我们使用then去拿结果的时候 需要异步的方式去拿 而then里的两个函数 就是我们的this.resolveFunction this.rejectFunction 如果我们只用var p1 = new MyPromise((resolve,reject) =&gt; &#123;resolve(ok)&#125;) 这个时候下面没有同步代码了 所以直接就把定时器里的代码拿出来执行了 但是当我们 p1.then((res) =&gt; &#123;&#125;,(reason) =&gt; &#123;&#125;) 的时候 then 里的执行函数 属于同步代码 在原型上 先做了 一些处理 然后 执行完了 在去宏任务里去看看 我究竟要执行哪个函数</span></span><br><span class="line">    state === <span class="string">&#x27;fulfilled&#x27;</span>? _this.resolveFunction(_this.MyPromiseValue):_this.rejectFunction(_this.MyPromiseValue)</span><br><span class="line">    &#125;,<span class="number">0</span>)</span><br><span class="line">    &#125;</span><br><span class="line">    executor(resolve,reject)</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//4. 所以我们需要在我们的原型上写一个then的方法 来拿到我们的值 因为原型重写了 所以我们要写我们构造器</span></span><br><span class="line">MyPromise.prototype = &#123;</span><br><span class="line">    <span class="title">constructor</span>:<span class="title">MyPromise</span>,</span><br><span class="line">    //5. 然后上面有一个<span class="title">then</span>方法 这个方法是异步的 接收两个函数 这个就是我们方面写的</span><br><span class="line">    <span class="title">then</span>:<span class="title">function</span> <span class="title">then</span>(<span class="params">resolveFunction,rejectFunction</span>)&#123;</span><br><span class="line">        <span class="comment">//6.我们这里还有一个顺延效果  当你已经改变了promise的状态 但是你then里面不传resolve的时候 或者 reject的时候 就会返回一个没有值的promise实例 </span></span><br><span class="line">        <span class="keyword">if</span>(<span class="keyword">typeof</span> resolveFunction !== <span class="string">&#x27;function&#x27;</span>)&#123;</span><br><span class="line">            resolveFunction = <span class="function"><span class="keyword">function</span> <span class="title">resolveFunction</span>(<span class="params">result</span>)</span>&#123;</span><br><span class="line">                <span class="keyword">return</span> MyPromise.resolve(result)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>(<span class="keyword">typeof</span> rejectFunction !== <span class="string">&#x27;function&#x27;</span>)&#123;</span><br><span class="line">            rejectFunction = <span class="function"><span class="keyword">function</span> <span class="title">rejectFunction</span>(<span class="params">result</span>)</span>&#123;</span><br><span class="line">                <span class="keyword">return</span> MyPromise.reject(result)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">this</span>.resolveFunction = resolveFunction</span><br><span class="line">        <span class="built_in">this</span>.rejectFunction = rejectFunction</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="then链的调用机制"><a href="#then链的调用机制" class="headerlink" title="then链的调用机制"></a>then链的调用机制</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 首先 当我们创建一个 promise 实例的时候 通过resolve 改变了成功的状态 然后可以通过then 里面的 第一个函数 拿到结果</span><br><span class="line"><span class="number">2.</span> 如果then里面的函数 我们不传 他会有一个顺延的的效果</span><br><span class="line"><span class="number">3.</span> 其实是返回一个 promise成功的实例 然后在通过then去调用</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> p1 = <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="keyword">function</span> (<span class="params">resolve,reject</span>) </span>&#123;</span><br><span class="line">  resolve(<span class="string">&#x27;OK&#x27;</span>)</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">//这里什么也没有传 但是我状态是成功的 所以 可以在下一个then中的第一个函数 再次拿到结果</span></span><br><span class="line">p1.then(<span class="literal">null</span>).then(<span class="function">(<span class="params">res</span>) =&gt;</span> &#123; <span class="built_in">console</span>.log(res) 输出ok&#125;)</span><br><span class="line"><span class="comment">//如果这里传了 一个别的值  那我们也需要判断 原路返回</span></span><br><span class="line">p1.then(<span class="function">(<span class="params">res</span>) =&gt;</span> &#123; <span class="keyword">return</span> <span class="number">10</span> 或者对象 或者别的什么)</span><br><span class="line"><span class="comment">// 如果这传了一个 promise 我们还需要从新调用then</span></span><br><span class="line">p1.then(<span class="function">(<span class="params">res</span>) =&gt;</span> &#123; <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;&#125;))</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">MyPromise</span>(<span class="params">executor</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(<span class="keyword">typeof</span> executor !== <span class="string">&#x27;function&#x27;</span>) <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">TypeError</span>(<span class="string">&#x27;MyPromise resolver undefined is not a function&#x27;</span>)</span><br><span class="line">    <span class="built_in">this</span>.MyPromiseState = <span class="string">&#x27;pending&#x27;</span></span><br><span class="line">    <span class="built_in">this</span>.MyPromiseValue = <span class="literal">undefined</span></span><br><span class="line">    <span class="built_in">this</span>.resolveFunction = <span class="function"><span class="keyword">function</span> <span class="title">rejectFunction</span> (<span class="params"></span>)  </span>&#123;&#125;</span><br><span class="line">    <span class="built_in">this</span>.rejectFunction = <span class="function"><span class="keyword">function</span> <span class="title">rejectFunction</span> (<span class="params"></span>) </span>&#123;&#125;</span><br><span class="line">    <span class="keyword">var</span> resolve = <span class="function"><span class="keyword">function</span> <span class="title">resolve</span> (<span class="params">result</span>)</span>&#123;</span><br><span class="line">        change(<span class="string">&#x27;fulfilled&#x27;</span>,result)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">var</span> reject = <span class="function"><span class="keyword">function</span> <span class="title">reject</span> (<span class="params">reason</span>)</span>&#123;</span><br><span class="line">        change(<span class="string">&#x27;rejected&#x27;</span>,reason)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">var</span> _this = <span class="built_in">this</span></span><br><span class="line">    <span class="keyword">var</span> change = <span class="function"><span class="keyword">function</span> <span class="title">change</span>(<span class="params">state,value</span>) </span></span><br><span class="line"><span class="function">    <span class="title">if</span>(<span class="params">_this.MyPromiseState !== <span class="string">&#x27;pending&#x27;</span></span>) <span class="title">return</span></span></span><br><span class="line"><span class="function">    <span class="title">_this</span>.<span class="title">MyPromiseState</span> = <span class="title">state</span></span></span><br><span class="line"><span class="function">    <span class="title">_this</span>.<span class="title">MyPromiseValue</span> = <span class="title">value</span></span></span><br><span class="line"><span class="function">    <span class="title">setTimeout</span>(<span class="params"><span class="keyword">function</span>()&#123;</span></span></span><br><span class="line"><span class="function"><span class="params">    state === <span class="string">&#x27;fulfilled&#x27;</span>? _this.resolveFunction(_this.MyPromiseValue):_this.rejectFunction(_this.MyPromiseValue)</span></span></span><br><span class="line"><span class="function"><span class="params">    &#125;,<span class="number">0</span></span>)</span></span><br><span class="line"><span class="function">    &#125;</span></span><br><span class="line"><span class="function">    <span class="title">executor</span>(<span class="params">resolve,reject</span>)</span></span><br><span class="line"><span class="function">&#125;</span></span><br><span class="line"><span class="function"><span class="title">MyPromise</span>.<span class="title">prototype</span> = </span>&#123;</span><br><span class="line">    <span class="title">constructor</span>:<span class="title">MyPromise</span>,</span><br><span class="line">    <span class="title">then</span>:<span class="title">function</span> <span class="title">then</span>(<span class="params">resolveFunction,rejectFunction</span>)&#123;</span><br><span class="line">        <span class="comment">//1.我们这里还有一个顺延效果  当你已经改变了promise的状态 但是你then里面不传resolve的时候 或者 reject的时候 就会返回一个没有值的promise实例 </span></span><br><span class="line">        <span class="comment">//在下个then中可以拿到值</span></span><br><span class="line">        <span class="keyword">if</span>(<span class="keyword">typeof</span> resolveFunction !== <span class="string">&#x27;function&#x27;</span>)&#123;</span><br><span class="line">            resolveFunction = <span class="function"><span class="keyword">function</span> <span class="title">resolveFunction</span>(<span class="params">result</span>)</span>&#123;</span><br><span class="line">                <span class="keyword">return</span> MyPromise.resolve(result)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>(<span class="keyword">typeof</span> rejectFunction !== <span class="string">&#x27;function&#x27;</span>)&#123;</span><br><span class="line">            rejectFunction = <span class="function"><span class="keyword">function</span> <span class="title">rejectFunction</span>(<span class="params">result</span>)</span>&#123;</span><br><span class="line">                <span class="keyword">return</span> MyPromise.reject(result)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//2. 这里为了能当状态改变成功或者失败之后 我们then 里面什么也不传  然后在在第二个then中调用 能拿到结果 所以我们还是要返回一个promise实例 </span></span><br><span class="line">    <span class="comment">//这样才能接着用then调用 为了保证this 指向正确我们缓存一下</span></span><br><span class="line">        <span class="keyword">var</span> _this = <span class="built_in">this</span></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> MyPromise(<span class="function"><span class="keyword">function</span>(<span class="params">resolve,reject</span>) </span>&#123;</span><br><span class="line">            <span class="comment">//3.这里由于我们不知道 究竟调用的是 成功的函数  还是 失败的函数 所以 这我们在把函数包装一层方便监控</span></span><br><span class="line">            _this.resolveFunction = <span class="function"><span class="keyword">function</span>(<span class="params">result</span>)</span>&#123;</span><br><span class="line">                <span class="comment">//4.这里有两种情况 第一种是 你自己有返回值 比如10 20 一个对象之类的 不是一个promise的成功的状态 我们直接返回就可以</span></span><br><span class="line">                <span class="comment">// 第二种 就是 你代码写的问题 直接报错了 所以我们要返回失败的状态</span></span><br><span class="line">                <span class="keyword">try</span>&#123;</span><br><span class="line">                    <span class="keyword">var</span> x = resolveFunction(result)</span><br><span class="line">                    <span class="comment">// 判断 你是返回的promise实例 我们就接着调用then 如果是特殊的值 我们就直接返回</span></span><br><span class="line">                    x <span class="keyword">instanceof</span> MyPromise? x.then(resolve,reject):resolve(x)</span><br><span class="line">                &#125;<span class="keyword">catch</span>(e)&#123;</span><br><span class="line">                    reject(e)</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            _this.rejectFunction = <span class="function"><span class="keyword">function</span>(<span class="params">reason</span>) </span>&#123;</span><br><span class="line">                <span class="keyword">try</span>&#123;</span><br><span class="line">                    <span class="keyword">var</span> x = rejectFunction(reason)</span><br><span class="line">                    <span class="comment">// 判断 你是返回的promise实例 还是特殊的值</span></span><br><span class="line">                    x <span class="keyword">instanceof</span> MyPromise? x.then(resolve,reject):resolve(x)</span><br><span class="line">                &#125;<span class="keyword">catch</span>(e)&#123;</span><br><span class="line">                    reject(e)</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">MyPromise.resolve = <span class="function"><span class="keyword">function</span> <span class="title">resolve</span> (<span class="params">result</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> MyPromise(<span class="function"><span class="keyword">function</span> (<span class="params">resolve</span>)</span>&#123;</span><br><span class="line">        resolve(result)</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br><span class="line">MyPromise.reject = <span class="function"><span class="keyword">function</span> <span class="title">reject</span> (<span class="params">reason</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> MyPromise(<span class="function"><span class="keyword">function</span>(<span class="params">_,reject</span>)</span>&#123;</span><br><span class="line">        reject(reason)</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> p1 = <span class="keyword">new</span> MyPromise(<span class="function">(<span class="params">resolve,reject</span>) =&gt;</span> &#123;</span><br><span class="line">    resolve(<span class="string">&#x27;ok&#x27;</span>)</span><br><span class="line">&#125;)</span><br><span class="line">p1.then().then(<span class="function">(<span class="params">res</span>) =&gt;</span> <span class="built_in">console</span>.log(res))</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="catch的方法"><a href="#catch的方法" class="headerlink" title="catch的方法"></a>catch的方法</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> <span class="keyword">catch</span> 的时候就比较简单了 直接返回一个失败的就好</span><br><span class="line"><span class="number">2.</span> 我们直接调用then 然后第一个参数不传 然后让他走第二个失败的函数</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">catch</span>:<span class="function"><span class="keyword">function</span>(<span class="params">rejectFunction</span>)</span>&#123;</span><br><span class="line">        <span class="comment">// xxx.catch(() =&gt; &#123;&#125;) =&gt; xxx.then(null,() =&gt; &#123;&#125;)</span></span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.then(<span class="literal">null</span>,rejectFunction)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="all方法"><a href="#all方法" class="headerlink" title="all方法"></a>all方法</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> <span class="built_in">Promise</span>.all([fn1(),fn2(),fn3(),<span class="number">10</span>]) 返回的也是promise实例 每个参数都是一个promise的实例</span><br><span class="line"><span class="number">2.</span> 只要有一个promise实例 是失败的 那么返回的整个状态就是失败</span><br><span class="line"><span class="number">3.</span> 如果最后都成功了 那么返回就是一个数组 里面是每个实例返回的值</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line">MyPromise.all = <span class="function"><span class="keyword">function</span>(<span class="params">promiseArr</span>) </span>&#123;</span><br><span class="line">    <span class="comment">//1. 先缓存下this</span></span><br><span class="line">    <span class="keyword">var</span> _this = <span class="built_in">this</span></span><br><span class="line">    <span class="comment">//2. 最后返回的也是一个promise的实例数组 值是有所有单个实例组成的数组</span></span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> MyPromise(<span class="function"><span class="keyword">function</span>(<span class="params">resolve,reject</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">var</span> index = <span class="number">0</span> <span class="comment">//用来计数</span></span><br><span class="line">            results = [] <span class="comment">//用来存放我们每次promise实例的结果</span></span><br><span class="line">        <span class="comment">//5. 这里我们还需要写一个通知的函数 当我们已经循环完了所有你传进来的数组 就我们就该把结果 拿出去了</span></span><br><span class="line">        <span class="keyword">var</span> fire = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">            <span class="keyword">if</span>(index &gt;= promiseArr.length)&#123;</span><br><span class="line">                resolve(results)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; promiseArr.length; i++)&#123;</span><br><span class="line">            <span class="comment">//3. 这个是每个实例</span></span><br><span class="line">            <span class="keyword">var</span> item = promiseArr[i]</span><br><span class="line">            <span class="comment">//4. index++ 开始计数</span></span><br><span class="line">            index++</span><br><span class="line"></span><br><span class="line">            <span class="comment">//5. 这里开始判断 如果你不是promise实例 那么我们就直接返还给你 并且结束当前 开始下一轮循环</span></span><br><span class="line">            <span class="keyword">if</span>(!(item <span class="keyword">instanceof</span> MyPromise))&#123;</span><br><span class="line">                results[i] = item</span><br><span class="line">                index++</span><br><span class="line">                <span class="comment">//看看 循环结束了么 如果结束了 我们就要返回结果了</span></span><br><span class="line">                fire()</span><br><span class="line">                <span class="keyword">continue</span></span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">//6. 如果不是promise实例 我们就要拿到它的返回结果 放进results</span></span><br><span class="line">            item.then(<span class="function"><span class="keyword">function</span>(<span class="params">res</span>)</span>&#123;</span><br><span class="line">                results[i] = res</span><br><span class="line">                index++</span><br><span class="line">                fire()</span><br><span class="line">            &#125;).catch(<span class="function"><span class="keyword">function</span>(<span class="params">reason</span>)</span>&#123;</span><br><span class="line">                <span class="comment">//7. 这里我们需要注意一下 这里只要有一个失败的 那么整体就失败了</span></span><br><span class="line">                reject(reason)</span><br><span class="line">            &#125;)</span><br><span class="line">            <span class="comment">//8.以上那么写 是会有错的 因为 你then 里面是异步的 这个时候循环已经结束了 i 的值 已经变成最后循环那轮结束的了 所以我们需要用闭包包起来 保证i的值是当前的</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 为了保证i 的准确 所以我们该用闭包的写法</span></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">MyPromise.all = <span class="function"><span class="keyword">function</span>(<span class="params">promiseArr</span>) </span>&#123;</span><br><span class="line">    <span class="comment">//1. 先缓存下this</span></span><br><span class="line">    <span class="keyword">var</span> _this = <span class="built_in">this</span></span><br><span class="line">    <span class="comment">//2. 最后返回的也是一个promise的实例数组 值是有所有单个实例组成的数组</span></span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> MyPromise(<span class="function"><span class="keyword">function</span>(<span class="params">resolve,reject</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">var</span> index = <span class="number">0</span> <span class="comment">//用来计数</span></span><br><span class="line">            results = [] <span class="comment">//用来存放我们每次promise实例的结果</span></span><br><span class="line">        <span class="comment">//5. 这里我们还需要写一个通知的函数 当我们已经循环完了所有你传进来的数组 并且结果都已经返回了 就我们就该把结果 拿出去了</span></span><br><span class="line">        <span class="keyword">var</span> fire = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">            <span class="keyword">if</span>(index &gt;= promiseArr.length)&#123;</span><br><span class="line">                resolve(results)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; promiseArr.length; i++)&#123;</span><br><span class="line">            (<span class="function"><span class="keyword">function</span>(<span class="params">i</span>)</span>&#123;</span><br><span class="line">            <span class="comment">//3. 这个是每个实例</span></span><br><span class="line">            <span class="keyword">var</span> item = promiseArr[i]</span><br><span class="line"></span><br><span class="line">            <span class="comment">//5. 这里开始判断 如果你不是promise实例 那么我们就直接返还给你 并且结束当前 开始下一轮循环</span></span><br><span class="line">            <span class="keyword">if</span>(!(item <span class="keyword">instanceof</span> MyPromise))&#123;</span><br><span class="line">                results[i] = item</span><br><span class="line">                index++</span><br><span class="line">                <span class="comment">//看看 循环结束了么 如果结束了 我们就要返回结果了</span></span><br><span class="line">                fire()</span><br><span class="line">                <span class="keyword">return</span></span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">//6. 如果不是promise实例 我们就要拿到它的返回结果 放进results</span></span><br><span class="line">            item.then(<span class="function"><span class="keyword">function</span>(<span class="params">res</span>)</span>&#123;</span><br><span class="line">                results[i] = res</span><br><span class="line">                index++</span><br><span class="line">                fire()</span><br><span class="line">            &#125;).catch(<span class="function"><span class="keyword">function</span>(<span class="params">reason</span>)</span>&#123;</span><br><span class="line">                reject(reason)</span><br><span class="line">            &#125;)</span><br><span class="line">            &#125;)(i)</span><br><span class="line">            <span class="comment">//8.以上那么写 是会有错的 因为 你then 里面是异步的 这个时候循环已经结束了 i 的值 已经变成最后循环那轮结束的了 所以我们需要用闭包包起来 保证i的值是当前的</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>完整代码</span><br><span class="line">https:<span class="comment">//github.com/wazer1987/easy_MyPromise</span></span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__title"><span class="post-copyright-info"><h>手写一个简版的promise</h></span></div><div class="post-copyright__type"><span class="post-copyright-info"><a href="http://example.com/2020/04/27/%E5%86%99%E4%B8%80%E4%B8%AA%E7%AE%80%E7%89%88%E7%9A%84Promise/">http://example.com/2020/04/27/%E5%86%99%E4%B8%80%E4%B8%AA%E7%AE%80%E7%89%88%E7%9A%84Promise/</a></span></div><div class="post-copyright-m"><div class="post-copyright-m-info"><div class="post-copyright-a"><h>作者</h><div class="post-copyright-cc-info"><h>北城韩雨</h></div></div><div class="post-copyright-c"><h>发布于</h><div class="post-copyright-cc-info"><h>2020-04-27</h></div></div><div class="post-copyright-u"><h>更新于</h><div class="post-copyright-cc-info"><h>2019-06-03</h></div></div><div class="post-copyright-c"><h>许可协议</h><div class="post-copyright-cc-info"><a class="icon" rel="noopener" target="_blank" title="Creative Commons" href="https://creativecommons.org/"><i class="fab fa-creative-commons"></i></a><a rel="noopener" target="_blank" title="CC BY-NC-SA 4.0" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a></div></div></div></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/JavaScript/">JavaScript</a></div><div class="post_share"></div></div><link rel="stylesheet" href="/" media="defer" onload="this.media='all'"/><div class="post-reward"><button class="tip-button reward-button"><span class="tip-button__text">打赏</span><div class="coin-wrapper"><div class="coin"><div class="coin__middle"></div><div class="coin__back"></div><div class="coin__front"></div></div></div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/img/weixin.jpg" target="_blank"><img class="post-qr-code-img" src="/img/weixin.jpg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="/img/taobao.jpg" target="_blank"><img class="post-qr-code-img" src="/img/taobao.jpg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></button></div><script defer="defer" src="/"></script><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2021/01/01/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E6%95%B0%E6%8D%AE%E5%93%8D%E5%BA%94%E5%BC%8F01/"><img class="prev-cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2017-07-20%2F4f06f9aa2ebcea6691a78537052e8e9f%2F00001.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530599&amp;t=32fb97bad7007bdc6c712ccadacd5721" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Vue源码开发-数据响应式01</div></div></a></div><div class="next-post pull-right"><a href="/2020/04/27/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/"><img class="next-cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com%2F20191017%2F4cfc341591d242327b67e9ceb6953612_00002.jpg&amp;refer=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530702&amp;t=80943e12d5239f63beafd6d1c540855f" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">发布订阅</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2020/04/27/设计模式/" title="发布订阅"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com%2F20191017%2F4cfc341591d242327b67e9ceb6953612_00002.jpg&refer=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530702&t=80943e12d5239f63beafd6d1c540855f" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-04-27</div><div class="title">发布订阅</div></div></a></div><div><a href="/2020/04/26/js事件循环机制/" title="JavaScript 事件循环机制"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530644&t=10679b5732c52e39eeb588040240a790" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-04-26</div><div class="title">JavaScript 事件循环机制</div></div></a></div><div><a href="/2020/04/22/ES6/" title="ES6/ES7/ES8/ES9/ES10/ES11"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2Faf41dabad727ba80c9ca8a2bfbd22a58%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530659&t=bbcfe1e86f393eecbb0722db1b92e475" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-04-22</div><div class="title">ES6/ES7/ES8/ES9/ES10/ES11</div></div></a></div><div><a href="/2020/04/01/事件绑定/" title="事件绑定"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2Faf41dabad727ba80c9ca8a2bfbd22a58%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530659&t=bbcfe1e86f393eecbb0722db1b92e475" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-04-01</div><div class="title">事件绑定</div></div></a></div><div><a href="/2020/03/29/Dialog/" title="弹框组件"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com%2F20191017%2F4cfc341591d242327b67e9ceb6953612_00002.jpg&refer=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530702&t=80943e12d5239f63beafd6d1c540855f" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-03-29</div><div class="title">弹框组件</div></div></a></div><div><a href="/2020/03/28/拖拽/" title="拖拽"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2016-12-29%2F0604092c7129acd0f4029c6728db7fd7%2F00001.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530724&t=333c5056a813a542419e5b661ebcfcd9" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-03-28</div><div class="title">拖拽</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/my.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">北城韩雨</div><div class="author-info__description">一个前端界的小学生</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">65</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">23</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">21</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/wazer1987"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="/wangzhengvslike@163.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content"><p>感谢访问本站，如喜欢请收藏。本站主要分享前端知识，立志成为资深前端工程师，但目前是一个前端界的小学生 若喜欢可以打赏请博主喝一杯冰阔落</p> <p>另外请大家多多支持淼哥的开源项目</p> <p>https://github.com/flipped-aurora/gin-vue-admin</p> <p>🌟🌟欢迎大家start 🌟🌟</p> <p>欢迎加入博主的前端技术交流群</p> <p style="background:rgba(255,255,255,1);margin:0"> <img src="/img/code.png"></img> </p></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%89%8B%E5%86%99%E4%B8%80%E4%B8%AA%E7%AE%80%E5%8D%95%E7%89%88%E7%9A%84Promise"><span class="toc-text">手写一个简单版的Promise</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Promise%E9%83%BD%E5%81%9A%E4%BA%86%E5%93%AA%E4%BA%9B%E4%BA%8B%E6%83%85"><span class="toc-text">Promise都做了哪些事情</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BD%9C%E4%B8%BA%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0"><span class="toc-text">作为构造函数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BD%9C%E4%B8%BA%E6%99%AE%E9%80%9A%E5%AF%B9%E8%B1%A1"><span class="toc-text">作为普通对象</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#then%E7%9A%84%E6%97%B6%E5%80%99%E6%89%A7%E8%A1%8C"><span class="toc-text">then的时候执行</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#then%E9%93%BE%E7%9A%84%E8%B0%83%E7%94%A8%E6%9C%BA%E5%88%B6"><span class="toc-text">then链的调用机制</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#catch%E7%9A%84%E6%96%B9%E6%B3%95"><span class="toc-text">catch的方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#all%E6%96%B9%E6%B3%95"><span class="toc-text">all方法</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2021/04/16/gin-vue-admin/" title="Gin-Vue-Admin"><img src="/img/gin-vue-admin.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Gin-Vue-Admin"/></a><div class="content"><a class="title" href="/2021/04/16/gin-vue-admin/" title="Gin-Vue-Admin">Gin-Vue-Admin</a><time datetime="2021-04-15T16:00:00.000Z" title="发表于 2021-04-16 00:00:00">2021-04-16</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/08/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E6%B8%B2%E6%9F%9310/" title="Vue源码开发-组件的渲染10"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530644&amp;t=10679b5732c52e39eeb588040240a790" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-组件的渲染10"/></a><div class="content"><a class="title" href="/2021/01/08/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E6%B8%B2%E6%9F%9310/" title="Vue源码开发-组件的渲染10">Vue源码开发-组件的渲染10</a><time datetime="2021-01-07T16:00:00.000Z" title="发表于 2021-01-08 00:00:00">2021-01-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/07/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E5%90%88%E5%B9%B6%E7%AD%96%E7%95%A509/" title="Vue源码开发-组件合并策略09"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com%2F20191017%2F4cfc341591d242327b67e9ceb6953612_00002.jpg&amp;refer=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530702&amp;t=80943e12d5239f63beafd6d1c540855f" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-组件合并策略09"/></a><div class="content"><a class="title" href="/2021/01/07/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E5%90%88%E5%B9%B6%E7%AD%96%E7%95%A509/" title="Vue源码开发-组件合并策略09">Vue源码开发-组件合并策略09</a><time datetime="2021-01-06T16:00:00.000Z" title="发表于 2021-01-07 00:00:00">2021-01-07</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E6%95%B0%E7%BB%84%E7%9A%84%E4%BE%9D%E8%B5%96%E6%94%B6%E9%9B%8608/" title="Vue源码开发-数组的依赖收集08"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530644&amp;t=10679b5732c52e39eeb588040240a790" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-数组的依赖收集08"/></a><div class="content"><a class="title" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E6%95%B0%E7%BB%84%E7%9A%84%E4%BE%9D%E8%B5%96%E6%94%B6%E9%9B%8608/" title="Vue源码开发-数组的依赖收集08">Vue源码开发-数组的依赖收集08</a><time datetime="2021-01-05T16:00:00.000Z" title="发表于 2021-01-06 00:00:00">2021-01-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E5%90%88%E5%B9%B607/" title="Vue源码开发-生命周期合并07"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2Faf41dabad727ba80c9ca8a2bfbd22a58%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530659&amp;t=bbcfe1e86f393eecbb0722db1b92e475" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-生命周期合并07"/></a><div class="content"><a class="title" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E5%90%88%E5%B9%B607/" title="Vue源码开发-生命周期合并07">Vue源码开发-生命周期合并07</a><time datetime="2021-01-05T16:00:00.000Z" title="发表于 2021-01-06 00:00:00">2021-01-06</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright"><span>&copy;2017 - 2021</span><svg style="width:2em; height:2em;position: relative;top: 10px;" aria-hidden="true"><use xlink:href="#icon-Butterfly"></use></svg><span>北城韩雨</span></div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text"><p><a style="margin-inline:5px" target="_blank" href="https://hexo.io/"><img src="https://img.shields.io/badge/Frame-Hexo-blue?style=flat&logo=hexo" title="博客框架为Hexo"></a><a style="margin-inline:5px" target="_blank" href="https://butterfly.js.org/"><img src="https://img.shields.io/badge/Theme-Butterfly-6513df?style=flat&logo=bitdefender" title="主题采用butterfly"></a><a style="margin-inline:5px" target="_blank" href="https://www.jsdelivr.com/"><img src="https://img.shields.io/badge/CDN-jsDelivr-orange?style=flat&logo=jsDelivr" title="本站使用JsDelivr为静态资源提供CDN加速"></a></p></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/medium-zoom/dist/medium-zoom.min.js"></script><div class="js-pjax"></div><script defer src="/live2d-widget/autoload.js"></script><script src="/js/coin.js"></script><script async src="//at.alicdn.com/t/font_2700632_3rtvhzzpl3h.js"></script><script src="/js/liuxing.js" data-pjax></script><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start --><div class="js-pjax"><script async="async">var arr = document.getElementsByClassName('recent-post-item');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '1s');
    arr[i].setAttribute('data-wow-delay', '0ms');
    arr[i].setAttribute('data-wow-offset', '100');
    arr[i].setAttribute('data-wow-iteration', '1');
  }</script><script async="async">var arr = document.getElementsByClassName('card-widget');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '');
    arr[i].setAttribute('data-wow-delay', '');
    arr[i].setAttribute('data-wow-offset', '');
    arr[i].setAttribute('data-wow-iteration', '');
  }</script></div><script defer src="https://cdn.jsdelivr.net/gh/graingert/wow@1.3.0/dist/wow.min.js"></script><script defer src="https://cdn.jsdelivr.net/npm/hexo-butterfly-wowjs/lib/wow_init.min.js"></script><script async src="//at.alicdn.com/t/font_2032782_8d5kxvn09md.js"></script><!-- hexo injector body_end end --></body></html>